package com.zrrd.jt_admin.common.response;

/**
 * @author WanJl
 * @version 1.0
 * @title ResponseResult
 * @description 响应结果类
 * @create 2024/3/24
 */
public class ResponseResult<O> {

    //响应给前端的状态码  20000添加成功，20001添加失败
    private Integer code;

    //响应给前端的数据
    private O data;

    //响应给前端的消息
    private String message;

    public ResponseResult() {
    }

    public ResponseResult(O data) {
        this.data = data;
    }

    public ResponseResult(Integer code, O data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(Integer code, O data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public O getData() {
        return data;
    }

    public void setData(O data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 查询的通用响应
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult queryResponse(T data) {
        if (data != null) {
            return new ResponseResult(ResponseStatusCode.FIND_OK.getCode(), data, ResponseStatusCode.FIND_OK.getMessage());
        } else {
            return new ResponseResult(ResponseStatusCode.FIND_ERR.getCode(), data, ResponseStatusCode.FIND_ERR.getMessage());
        }
    }
    public static <T> ResponseResult queryResponse(T data,String successMessage, String failMessage) {
        if (data != null) {
            return new ResponseResult(ResponseStatusCode.FIND_OK.getCode(), data, successMessage);
        } else {
            return new ResponseResult(ResponseStatusCode.FIND_ERR.getCode(), data, failMessage);
        }
    }
    public static <T> ResponseResult defaultResponse(T data, String successMessage, String failMessage) {
        if (data != null) {
            return new ResponseResult(ResponseStatusCode.DEFAULT_OK.getCode(), data, successMessage);
        } else {
            return new ResponseResult(ResponseStatusCode.DEFAULT_ERR.getCode(), data, failMessage);
        }
    }

    /**
     * 删除通用响应
     * @param flag
     * @return
     */
    public static ResponseResult removeResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.DELETE_OK.getCode(), flag, ResponseStatusCode.DELETE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.DELETE_ERR.getCode(), flag, ResponseStatusCode.DELETE_ERR.getMessage());
        }
    }

    /**
     * 添加通用响应
     * @param flag
     * @return
     */
    public static ResponseResult addResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.SAVE_OK.getCode(), flag, ResponseStatusCode.SAVE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.SAVE_ERR.getCode(), flag, ResponseStatusCode.SAVE_ERR.getMessage());
        }
    }

    /**
     * 修改通用响应
     * @param flag
     * @return
     */
    public static ResponseResult updateResponse(boolean flag) {
        if (flag) {
            return new ResponseResult(ResponseStatusCode.UPDATE_OK.getCode(), flag, ResponseStatusCode.UPDATE_OK.getMessage());

        } else {
            return new ResponseResult(ResponseStatusCode.UPDATE_ERR.getCode(), flag, ResponseStatusCode.UPDATE_ERR.getMessage());
        }
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }

}
